﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LIB;
using System.Data.SqlClient;

namespace Web.myadmin
{
    public partial class Login : System.Web.UI.Page
    {
        BZZ.DAL.adminUsers dal = new BZZ.DAL.adminUsers();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void imbLogin_Click(object sender, ImageClickEventArgs e)
        {
            string userName = PageValidate.InputText(this.txtUserName.Text.Trim(), 20);
            string pwd = PageValidate.InputText(this.txtPassword.Text.Trim(), 20);

            if (userName == "")
            {
                MessageBox.Show(this,"请输入用户名");
                return;
            }
            if (pwd == "")
            {
                MessageBox.Show(this, "请输入密码");
                return;
            }
            //if (checkCode == "")
            //{
            //    MessageBox.Show(this, "请输入验证码");
            //    return;
            //}
            //if (checkCode.ToUpper() != Session["CheckCode"].ToString())
            //{
            //    MessageBox.Show(this, "验证码输入错误");
            //    return;
            //}
            var resStr = "帐号：" + userName + "；密码：" + pwd;
            List<SqlParameter> para = new List<SqlParameter>();
            var sql = "username=@username and password=@password";
            var EPwd = MD5Pwd.EPwd(pwd).ToUpper();

            para.Add(new SqlParameter("@username",userName));
            para.Add(new SqlParameter("@password", EPwd));

            BZZ.Model.adminUsers model = dal.GetModel(sql, para);
            if(model!=null)
            {
                if (model.status == 0)
                {
                    MessageBox.Show(this, "对不起您的帐号已被冻结");
                    SetLog(0, "N", resStr);
                    return;
                }
                SessionHelp.SetLoginUser(this, model);
                SetLog(SessionHelp.GetLoginUser(this).id, "Y", "登录成功");
                SetLogin(SessionHelp.GetLoginUser(this).id);
                Response.Redirect("Main.aspx");
            }
            else
            {
                MessageBox.Show(this,"帐号或密码错误");
                SetLog(0, "N", resStr);
            }
        }

        //登录成功之后写入登录信息
        private void SetLogin(int id)
        {
            var sql = "update bzz_adminUsers set lastloginTime='" + DateTime.Now + "',lastloginip='" + GetIp() + "',loginnum=loginnum+1 where id =" + id;
            dal.Update(sql,null);
        }

        //设置登录日志
        private void SetLog(int adminID,string result,string ext)
        {
            BZZ.Web.SetLog.SetAdminLog("用户登录", adminID, GetIp(), result, ext);
        }

        //获取登录的ip
        private string GetIp()
        {
            string userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (userIP == null || userIP == "")
            {
                userIP = Request.ServerVariables["REMOTE_ADDR"];
            }
            return userIP;
        }
    }
}